<?php

namespace Beerus\Modules\DesignerService\Actions;

use Doraemon\model\DesignerCompetition\DcAward;
use Doraemon\model\DesignerService\ServiceAuditLog;
use Doraemon\model\DesignerService\ServiceContentRecord;
use Doraemon\tools\AppMsgSend;
use Doraemon\tools\Hhzurl;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class ServiceFail extends RowAction
{
	public $name = '不通过';
	const TOAST_ALERT = '确定审核不通过吗？';

	public function handle($id, Request $request) {
        $uid = $request->post('uid');
        $auditRemark = $request->post('audit_remark');
		$result = ServiceContentRecord::update([
			'audit_status' => ServiceContentRecord::AUDIT_FAIL,
			'audit_remark' => $auditRemark,
			'edit_time' => time(),
			'admin_id' => Admin::user()->id,
			'admin_name' => Admin::user()->username
		], [
			'id' => $id
		]);
		if ($result) {
			ServiceAuditLog::insert([
				'service_content_id' => $id,
				'op' => ServiceAuditLog::OP_FAIL,
				'admin_id' => Admin::user()->id,
				'admin_name' => Admin::user()->username
			]);
			// 给设计师发送下行消息
            $msg = "很遗憾，你发布的服务流程由于「{$auditRemark}」，没有通过审核，建议修改后重新发布。如有疑问可咨询辅导员（APP-我的-联系运营辅导员）。";
            AppMsgSend::sendMsgNoTitle($uid, $msg);
			return $this->response()->success('操作成功(＾－＾)V')->refresh();
		}else{
			return $this->response()->success('操作失败,请稍后重试o(TωT)o');
		}
	}

	public function retrieveModel(Request $request)
	{
		if (!$key = $request->get('_key')) {
			return false;
		}

		return $key;
	}

    public function form()
    {
        $this->hidden('uid')->value($this->row->uid);
        $this->text('audit_remark', '理由')->required();
    }
}
